// pages/camel_card/activation/index.js
var app = getApp();
var time_box;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone_text: '',
    verif_text: '',
    // 60秒倒计时
    times: '',
    // 模态框
    modal_box: false,
    modal_types: 1,  // 0为激活失败，1为激活成功
    Activation_fails: 1,  //  激活失败时   1为还未购买过骆驼的商品  2为购买过，但未满299元
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  // 用户点击获取验证码
  verification: function () {
    var that = this;
    if (!(/^1\d{10}$/.test(that.data.phone_text))) {
      prompt('手机号错误', 'loading', false); return;
    }
    if (that.data.times == '') {
      that.setData({ times: 60, })
      getLocalTime(that, that.data.times);
      that.onShow();
      wx.request({
        url: app.url + 'api_ucenter/send_sms',
        data: {
          mobile: that.data.phone_text,
          type: 'activateCard',
        },
        header: {
          'content-type': 'application/json',
          'token': wx.getStorageSync('token_key')
        }, success: function (json) {
          console.log(json)
          if (!json.data.done) {
            that.setData({ times: '', })
            clearInterval(time_box);
            time_box = '';
            prompt(json.data.message, 'loading', true);
          }
        }
      })

    } else {
      prompt('请稍后点击', 'loading', true);
    }
  },
  // 用户输入  手机号 and 验证码
  text_input: function (e) {
    var that = this;
    //保存用户手机号
    if (e.currentTarget.dataset.name == "phone")
      that.setData({ phone_text: e.detail.value, });

    //保存验证码
    if (e.currentTarget.dataset.name == "verif")
      that.setData({ verif_text: e.detail.value, });

  },
  // 清空input
  delete_icon: function (e) {
    var that = this;
    console.log(e);
    if (e.currentTarget.dataset.name == "phone") that.setData({ phone_text: '', });
    else if (e.currentTarget.dataset.name == "verif") that.setData({ verif_text: '', });
  },
  // 表单提交
  formSubmit: function (e) {
    var that = this;
    var phone = e.detail.value.phone;//获取用户输入的 手机号
    var verif = e.detail.value.verif;//获取用户输入的 验证码
    if (!(/^1\d{10}$/.test(that.data.phone_text))) {
      prompt('手机号错误', 'loading', false); return;
    }
    /*if (that.data.times == '') {
      prompt('请获取验证码', 'loading', false); return;
    }*/
    if (that.data.verif_text == '') {
      prompt('请输入验证码', 'loading', false); return;
    }

    wx.request({
      url: app.url + 'api_ucenter/activateCard',
      data: {
        phoneNumber: phone,
        smsCode: verif,
      },
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token_key')
      }, success: function (json) {
        console.log(json)
          var modal_types = 1, Activation_fails = 1;
        if (json.data.done == true) {
          that.setData({ modal_box: true, modal_types: 1, })
          setTimeout(() => {
            that.setData({ modal_box: false });
            wx.redirectTo({ url: '/pages/camel_card/my_camelCard/index' })
          }, 2000)

        } else {
          if (json.data.error == 3) {
            prompt(json.data.message, 'loading', true);

          } else {
            that.setData({ modal_box: true, modal_types: 0, Activation_fails: json.data.error })
          }
        }
      }

    })


  },
  // 激活失败时
  go_home: function () {
    var that = this;
    that.setData({ modal_box: false, })
    wx.reLaunch({
      url: '/pages/tabBar/index/index',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
   onShow: function () {
    var that = this;
    if (that.data.times > 0) {
      time_box = setInterval(function () {
        getLocalTime(that, that.data.times)
      }, 1000)
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
})

// 提示
function prompt(title, status, mask) {
  wx.showToast({
    title: title,
    icon: status,
    duration: 1200,
    mask: mask
  })
}


// 获取验证码倒计时
function getLocalTime(that, nS) {
  if (nS != 0) {
    --nS;
    var S;
    function fillZero(S,v) {
      if ( v < 10 ) {
        if(v == 0){
          v = '0';
        } else {
          v = '0' + v;
        }
      }
      return v;
    }

    var d = new Date(parseInt(nS) * 1000);
    S = fillZero(S, d.getSeconds());
    that.setData({ times: S, })
  } else {
    that.setData({ times: '', })
    clearInterval(time_box);
    time_box = '';
  }
}